package com.songcha.module_mine.ui.fragment.mine

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.beanFlatMap
import com.songcha.library_business.api.ApiManager
import com.songcha.library_business.bean.book.BookBean
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_business.proxy.database.BookDetailDaoManager
import com.songcha.library_business.proxy.database.BookLastReadDaoManager
import com.songcha.library_business.proxy.database.TodayObtainGoldCountDaoManager
import com.songcha.library_business.proxy.database.BookTodayReadTimeDaoManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_business.viewmodel.BaseErrorRefreshViewModel
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_network.bean.BaseListDataBean
import com.songcha.module_mine.bean.GoldExchangeVipBean
import com.songcha.module_mine.bean.SystemMessageDataBean

class MineViewModel(app: Application):BaseErrorRefreshViewModel<MineRepository,String>(app) {
    var messageCount=MutableLiveData<Int>()
    var lastMsgTime=0L
    var goldExchangeVipList=MutableLiveData<MutableList<GoldExchangeVipBean>>()
    var goldExchangeState=MutableLiveData<Boolean>()
    var currentExchange:GoldExchangeVipBean?=null
    //var totalGold=MutableLiveData(0)
    var todayGold=MutableLiveData<Int>()
    var todayRead=MutableLiveData<Int>()

    fun getTodayGold(){
        todayGold.value=TodayObtainGoldCountDaoManager.getTodayObtainGoldCount()
    }

    fun getTodayRead(){
        todayRead.value=BookTodayReadTimeDaoManager.getTodayReadTime()
    }

    fun continueRead(){
        val bookId= BookLastReadDaoManager.getLastReadBookId()
        if(bookId.isBlank()){
            ToastUtil.show("暂无阅读书籍")
            return
        }
        val detailList= BookDetailDaoManager.getBookdetailListByIdList(mutableListOf(bookId))
        if(detailList.size==0){
            ToastUtil.show("暂无书籍信息")
            return
        }

        val item=detailList[0]
        val book= BookBean().apply {
            id=item.bookId
            thumb=item.thumb
            bookName=item.name
            author=item.author
            textCount=item.textCount
        }
        BusinessJumpUtil.toBookReader(book)
    }

    fun getGoldExchangeVipList(){
        handleApiDataObserver(repository!!.getGoldExchangeVipList(),object :
            IApiResult<BaseListDataBean<GoldExchangeVipBean>> {
            override fun onSuccess(bean: BaseListDataBean<GoldExchangeVipBean>) {
                goldExchangeVipList.value=bean.data
            }
        })
    }

    fun goldExchangeVip(index:Int){
        if(goldExchangeVipList.value==null) return
        if(goldExchangeVipList.value!!.size-1<index) return
        currentExchange=goldExchangeVipList.value!![index]
        val obs=repository!!.goldExchangeVip(currentExchange!!.id)
            .beanFlatMap {
                return@beanFlatMap ApiManager.api.getUserInfo()
            }

        handleApiDataObserver(obs, object :IApiResult<UserInfoBean>{
            override fun onSuccess(bean: UserInfoBean) {
                UserInfoManager.saveUserInfo(bean.data)
                goldExchangeState.value=true
            }

            override fun onError(e: Throwable) {
                goldExchangeState.value=false
            }

        })
    }


    fun getSystemMessage(){
        var obs=repository!!.getSystemMessage()

        if(UserInfoManager.checkLogin()){
            obs=ApiManager.api.getUserInfo()
                .beanFlatMap {
                    UserInfoManager.saveUserInfo(it.data)
                    return@beanFlatMap repository!!.getSystemMessage()
                }
        }

        handleApiDataObserver(obs,object :
            IApiResult<SystemMessageDataBean> {
            override fun onSuccess(bean: SystemMessageDataBean) {
                handleDataListSuccess(mutableListOf(""))
                if(bean.data.size>0){
                    if(bean.data[0].createTime==null) return
                    lastMsgTime=TimeUtil.timeString2Stamp(bean.data[0].createTime)
                    messageCount.value=bean.data.size
                }

            }
        },true)
    }
}